package com.voxel.sense.infomsg.module.task.operation;

import com.example.com.common.factory.data.DataSource;
import com.example.com.common.utils.NetworkUtils;
import com.example.com.common.utils.ToastUtil;
import com.example.com.common.view.recycler.RecyclerAdapter;
import com.voxel.sense.infomsg.App;
import com.voxel.sense.infomsg.R;
import com.voxel.sense.infomsg.module.task.BaseTaskPresenter;
import com.voxel.sense.infomsg.net.helper.TaskManagerNetHelper;

import java.util.List;

import cyy.example.com.facotry.Factory;
import cyy.example.com.facotry.constants.Constants;
import cyy.example.com.facotry.model.api.BaseEntity;
import cyy.example.com.facotry.model.api.response.MyTaskListInfoEntity;
import cyy.example.com.facotry.model.api.response.TaskStatuEntity;
import cyy.example.com.facotry.net.RequestIdFactory;

/**
 * Created by ChenYiYao on 2018/3/22.
 */

public class TaskOperatePresenter extends BaseTaskPresenter<TaskOperateContract.View>
        implements TaskOperateContract.Presenter, DataSource.Callback<BaseEntity> {
    private String mType;

    public TaskOperatePresenter(TaskOperateContract.View view) {
        super(view);
    }

    @Override
    public void loadData() {
        netloadData(1);
    }

    @Override
    public void loadMoreData() {
        netloadData(CURRENT_PAGE_SIZE + 1);
    }


    private void netloadData(int numb) {
        TaskManagerNetHelper.queryMyTaskInfoList(numb, mType, mSortType,
                (List<String>) mQueryMap.get(Constants.TYPE_AREA), (List<String>)
                        mQueryMap.get(Constants.TYPE_IDUSTRY), this);
    }

    @Override
    public void onDataLoaded(BaseEntity entity, int reqId) {
        if (mView == null) {
            return;
        }
        mView.dimissLoading();
        if (reqId == RequestIdFactory.GET_TASK_INFO_LIST) {
            //获取任务列表
            loadTaskListFinish((MyTaskListInfoEntity) entity);
        } else if (reqId == RequestIdFactory.ACCEPT_TASK) {//接收任务
            acceptSuccess(entity);
        }
    }

    private void loadTaskListFinish(MyTaskListInfoEntity entity) {
        MyTaskListInfoEntity data = entity;
        if (data != null) {
            CURRENT_PAGE_SIZE = data.getBody().getPageNum();
            loadDataComplete(data.getBody().getTaskInfoList());
        } else {
            LoadDataFailure();
        }
    }

    @Override
    public void onDataNotAvailable(String strRes, int reqId) {
        if (mView == null) {
            return;
        }
        mView.dimissLoading();
        LoadDataFailure();
        mView.showEmptyView(strRes,true);
    }


    //接收任务成功
    private void acceptSuccess(BaseEntity data) {
        //接收任务成功后，删除相应的item
        ToastUtil.showToastLong(Factory.app(), "任务接收成功");
        TaskStatuEntity entity = (TaskStatuEntity) data;
        RecyclerAdapter<MyTaskListInfoEntity.BodyBean.TaskInfoListBean> a = mView.getRecyclerViewAdapter();
        List<MyTaskListInfoEntity.BodyBean.TaskInfoListBean> dataList = a.getItems();
        for (int i = dataList.size() - 1; i >= 0; i--) {
            MyTaskListInfoEntity.BodyBean.TaskInfoListBean bean = dataList.get(i);
            if (bean.getTaskNumber().equals(entity.getBody().getTaskNumber())) {
                dataList.remove(i);
                mView.UpTaskStatus();
                return;
            }
        }
    }

    @Override
    public void doOperateTask(int type, int posision) {
        if (!NetworkUtils.isConnected()){
            ToastUtil.showToastLong(App.getInstance().getApplicationContext(), R.string.network_unavailable);
            return;
        }
        List<MyTaskListInfoEntity.BodyBean.TaskInfoListBean> items = mView.getRecyclerViewAdapter().getItems();
//        if (type == Constants.TASK_UNACCEPT_TYPE) {//未接收
//            L.d("接收任务");
//            MyTaskListInfoEntity.BodyBean.TaskInfoListBean taskInfoListBean = items.get(0);
//            TaskManagerNetHelper.acceptTask(taskInfoListBean.getTaskNumber(), this);
//        } else if (type == Constants.TASK_FINISH_TYPE) {//执行完成审核状态/
//            mView.onCheckTask(items.get(posision));
//        } else if (type == Constants.TASK_ACTION_TYPE) { //执行任务
//            mView.onExecuteTask(items.get(posision));
//        }
    }

    @Override
    public void setTaskType(int type) {
//        this.mType = type;
    }
}
